<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>猜数字游戏</title>
    <script>
      function startGame() {
        //定义变量保存猜测的次数范围
        let start = 1,
          end = 100
        //定义变量保存猜测次数
        let count = 1
        //产生1~100的随机整数
        let randomNumber = Math.floor(Math.random() * 100 + 1)
        console.log(randomNumber)
        let guessNumber = prompt(`请您输入一个数字[${start}-${end}]: `)
        for (;;) {
          if (guessNumber > randomNumber) {
            count++
            end = guessNumber
            guessNumber = prompt(`太大了,请您输入一个数字[${start}-${end}]: `)
          } else if (guessNumber < randomNumber) {
            count++
            start = guessNumber
            guessNumber = prompt(`太小了,请您输入一个数字[${start}-${end}]: `)
          } else {
            break
          }
        }
        document.write(`恭喜您猜对了,随机数为: ${randomNumber},您一共猜测了${count}次.`)
      }

      startGame()
    </script>
  </head>
  <body></body>
</html>
